package com.wswy.wzcx.ui.car.remind

import android.arch.lifecycle.Observer
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import com.che.libcommon.ui.NavTabController
import com.wswy.wzcx.R
import com.wswy.wzcx.model.license.UserCarInfo
import com.wswy.wzcx.module.base.CBaseActivity
import com.wswy.wzcx.ui.Constants

class RemindActivity : CBaseActivity(), NavTabController.FragmentProvider {

    companion object {
        private const val TAG = "RemindActivity"
        private const val FRG_TAG_ADD = "frg_add"
        private const val FRG_TAG_INFO = "frg_info"

        private const val TYPE_INSURANCE = 1  //保险
        private const val TYPE_INSPECTION = 2 //年检
        private const val TYPE_VALUATION = 3  //估值

        fun getInsuranceIntent(context: Context):Intent = Intent(context, RemindActivity::class.java).apply {
            putExtra(Constants.EXTRA_TYPE, TYPE_INSURANCE)
        }
        fun getInspectionIntent(context: Context):Intent = Intent(context, RemindActivity::class.java).apply {
            putExtra(Constants.EXTRA_TYPE, TYPE_INSPECTION)
        }

        fun startInsurance(context: Context, userCarInfo: UserCarInfo?,carId:Int?=0) {
            context.startActivity(getInsuranceIntent(context).apply {
                putExtra(Constants.EXTRA_DATA_CHOOSE_CAR, userCarInfo)
                putExtra(Constants.EXTRA_USER_CAR_ID,carId)
            })
        }

        fun startInspection(context: Context, userCarInfo: UserCarInfo?,carId:Int?=0,forceEdit:Boolean?=false) {
            context.startActivity(getInspectionIntent(context).apply {
                putExtra(Constants.EXTRA_DATA_CHOOSE_CAR, userCarInfo)
                putExtra(Constants.EXTRA_USER_CAR_ID,carId)
                putExtra("edit",forceEdit)
            })
        }

        fun startValuation(context: Context,userCarInfo: UserCarInfo?,carId:Int?=0){
            context.startActivity(Intent(context, RemindActivity::class.java).apply {
                putExtra(Constants.EXTRA_TYPE, TYPE_VALUATION)
                putExtra(Constants.EXTRA_DATA_CHOOSE_CAR, userCarInfo)
                putExtra(Constants.EXTRA_USER_CAR_ID,carId)
            })
        }

    }

    private lateinit var navTabController: NavTabController
    private lateinit var remindViewModel: RemindViewModel

    private var showType: Int = 1

    private var forceEdit =false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_sample)

        showType = intent.getIntExtra(Constants.EXTRA_TYPE,TYPE_INSURANCE)

        forceEdit =intent?.getBooleanExtra("edit",false)?:false

        navTabController = NavTabController(this, supportFragmentManager, R.id.fl_container)
        navTabController.setOnChangeCallback {
            when (it) {
                FRG_TAG_ADD -> {

                    when(showType){
                        TYPE_INSURANCE -> setAppTitle("添加保险提醒")
                        TYPE_INSPECTION -> setAppTitle("添加年检提醒")
                        TYPE_VALUATION -> setAppTitle("添加车辆估值提醒")
                    }

                }
                FRG_TAG_INFO -> {

                    when(showType){
                        TYPE_INSURANCE -> setAppTitle("保险提醒")
                        TYPE_INSPECTION -> setAppTitle("年检提醒")
                        TYPE_VALUATION -> setAppTitle("估值报告")
                    }

                }
            }
        }

        remindViewModel = provideViewModel(RemindViewModel::class.java)

        remindViewModel.getUserCarInfoLiveData().observe(this, Observer {

            it?.run {
                show(this,forceEdit)
            }

        })

        val tmpUserCarInfo = intent.getParcelableExtra<UserCarInfo?>(Constants.EXTRA_DATA_CHOOSE_CAR)
        val tmpCarId = intent.getIntExtra(Constants.EXTRA_USER_CAR_ID,0)

        if (tmpUserCarInfo == null && tmpCarId == 0){
            finish()
            return
        }

        //如果有车辆信息，直接显示
        tmpUserCarInfo?.run {
            remindViewModel.setCarInfo(this)
        }?:kotlin.run {
            //没有的话，根据车辆id查询
            if (tmpCarId > 0){
                remindViewModel.loadCarInfo(tmpCarId)
            }else{
                finish()
            }
        }
    }

    //显示和切换信息
    private fun show(userCarInfo: UserCarInfo?,forceEdit: Boolean=false){
        userCarInfo?.run {

            when(showType){

                TYPE_INSURANCE -> {
                    if (carExtendInfo?.insuranceInfo != null && !forceEdit) {
                        showInfo()
                    } else {
                        showAdd()
                    }
                }
                TYPE_INSPECTION -> {
                    if (carExtendInfo?.inspection != null && !forceEdit) {
                        showInfo()
                    } else {
                        showAdd()
                    }
                }
                TYPE_VALUATION -> {
                    if (carExtendInfo?.valuation?.info != null && !forceEdit) {
                        showInfo()
                    } else {
                        showAdd()
                    }
                }
            }
        }

    }

    override fun getFragment(tag: String?): Fragment? {
        return when (tag) {
            FRG_TAG_ADD -> {

                when(showType){
                    TYPE_INSURANCE -> InsuranceAddFragment()
                    TYPE_INSPECTION -> InspectionNewAddFragment()
                    TYPE_VALUATION -> ValuationAddFragment()
                    else -> null
                }

            }

            FRG_TAG_INFO -> {
                when(showType){
                    TYPE_INSURANCE -> InsuranceInfoFragment()
                    TYPE_INSPECTION -> InspectionInfoFragment()
                    TYPE_VALUATION -> ValuationInfoFragment()
                    else -> null
                }
            }

            else -> null
        }
    }

    fun showAdd() {
        navTabController.show(FRG_TAG_ADD)
    }

    fun showInfo() {
        navTabController.show(FRG_TAG_INFO)
    }

    override fun onBackPressed() {

        if ( navTabController.isShowing(FRG_TAG_ADD)) {

            val handle = when(showType){

                TYPE_INSURANCE -> remindViewModel.userCarInfo.carExtendInfo?.insuranceInfo != null

                TYPE_INSPECTION -> remindViewModel.userCarInfo.carExtendInfo?.inspection != null

                TYPE_VALUATION -> remindViewModel.userCarInfo.carExtendInfo?.valuation != null
                else -> false
            }
            if (handle && !forceEdit){
                showInfo()
            }else{
                super.onBackPressed()
            }

        } else {
            super.onBackPressed()
        }

    }

}